import 'dart:convert';

import 'package:dio/dio.dart';
/**
 * 更改密码页面
 * author wim
 */
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/LoadingUtil.dart';
import 'package:panda/util/SignUtil.dart';
import '../base/Constant.dart';
import 'dart:ui';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ChangePwdPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ChangePwdView();
  }
}

class ChangePwdView extends State<ChangePwdPage> {
  String token = "";
  String pwd_old = "", pwd1 = "", pwd2 = "";
  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "修改密码",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.2,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      resizeToAvoidBottomPadding: false, //输入框抵住键盘
      body: new Container(
        color: divideColor,
        child: new Column(
          children: <Widget>[
            new Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20)),
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('原密码:',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      maxLines: 1,
                      cursorColor: mainColor,
                      onChanged: (value) {
                        pwd_old = value;
                      },
                      keyboardAppearance: Brightness.light,
                      decoration: new InputDecoration(
                          hintText: "",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  5,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20)),
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text(
                      '新密码:',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      maxLines: 1,
                      cursorColor: mainColor,
                      onChanged: (value) {
                        pwd1 = value;
                      },
                      keyboardAppearance: Brightness.light,
                      decoration: new InputDecoration(
                          hintText: "",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_10),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20)),
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('重复密码:',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      maxLines: 1,
                      keyboardAppearance: Brightness.light,
                      cursorColor: mainColor,
                      onChanged: (value) {
                        pwd2 = value;
                      },
                      decoration: new InputDecoration(
                          hintText: "",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              width: double.infinity,
              height: ScreenUtil.getInstance().setHeight(DP_80),
              margin: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_80)),
              decoration: new BoxDecoration(
                 color: mainColor,
                  borderRadius: BorderRadius.circular(
                      ScreenUtil.getInstance().setHeight(DP_80))),
              padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              child: new FlatButton(
                child: new Padding(
                  padding: EdgeInsets.all(0.0),
                  child: new Text(
                    "确认修改",
                    style: new TextStyle(
                        color: Colors.white,
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  ),
                ),
                onPressed: () {
                  showDialog(
                      context: context,
                      barrierDismissible: false,
                      builder: (_) {
                        return new NetLoadingDialog(
                          loadingText: "修改中...",
                          bgColor: Color.fromARGB(255, 0, 0, 0),
                          requestCallBack: null,
                          outsideDismiss: false,
                        );
                      });
                  register();
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  register() async {
    if (pwd_old == "") {
      Navigator.of(context).pop(1);
      return;
    }
    if (pwd1 == "") {
      Navigator.of(context).pop(1);
      return;
    }
    if (pwd2 == "") {
      Navigator.of(context).pop(1);
      return;
    }
    Map map = new Map();
    map['token'] = token;
    map['oldpwd'] = pwd_old;
    map['pwd1'] = pwd1;
    map['pwd2'] = pwd2;
    map['sign_type'] = SignType.CHANGE_PWD;
    Response<String> response =
        await HttpUtil().post(Api.CHANGE_PWD, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      Navigator.of(context).pop();
      Navigator.of(context).pop();
    }
  }
}
